package com.cci.kangdao.utilTool;

import org.apache.log4j.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

/**
 * 时间工具类
 * @author Eric Mu
 *
 */
public class LocalTimeUtil {

	private static Logger log = Logger.getLogger(LocalTimeUtil.class);

	private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
	
	private static final DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
	
	public static final String DTF_FORMATTER = "yyyy-MM-dd HH:mm:ss";
	public static final String DTF_DAY_FORMATTER = "yyyyMMdd";

	public static final String DTF_DATE_FORMATTER = "yyyy-MM-dd";

	public static final String DATE_FORMATTER = "yyyyMM";
	/**
	 * 获取当前时间字符串格式
	 * @return
	 */
	public static String getCurrentTime() {
		
		return LocalDateTime.now(ZoneId.of("Asia/Shanghai")).format(dtf);
	}
	
	/**
	 * 获取实际当前时间(服务器时间加8小时)
	 * @return
	 */
	public static String getRealCurrentTime() {
		return getFormatterRealCurrentTime(DTF_FORMATTER);
	}

	public static String getFormatterRealCurrentTime(String formatter) {
		//设置Date输出格式
		SimpleDateFormat sdf = new SimpleDateFormat(formatter);
		Date chinaCurrentTime = new Date();
		TimeZone zoneChina = TimeZone.getTimeZone("Asia/Shanghai");
		sdf.setTimeZone(zoneChina);
		return sdf.format(chinaCurrentTime);
		
	}
	
	/**
	 * 获取当前的北京日期(不包含时间)
	 */
	public static String getRealCurrentDate() {
		return LocalDate.now(ZoneId.of("Asia/Shanghai")).format(dtf1);
	}
	
	
	/**
	 * 获取当前的北京日期(不包含时间)
	 * 
	 */
	public static LocalDate getRealCurrentLocalDate() {
		return LocalDate.now(ZoneId.of("Asia/Shanghai"));
	}

	/**
	 * 获取当前的北京日期(不包含时间)
	 *
	 */
	public static LocalDateTime getRealCurrentLocalDateTime() {
		return LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
	}
	
	
	/**
	 * 当前的北京日期加上指定的年数
	 */
	public static String getCurrentDateAddYears(long years) {
		return getRealCurrentLocalDate().plusYears(years).format(dtf1);
	}
	
	
	/**
	 * 校验指定的日期是否已过期
	 * @param date是不包含时间的 格式 yyyy-MM-dd
	 */
	public static boolean isExpired(String date) {
		LocalDate expireDate = LocalDate.parse(date, dtf1);
		LocalDate nowDate = LocalDate.now(ZoneId.of("Asia/Shanghai"));
		return nowDate.isAfter(expireDate);
	}
	
	
	public static String getFormatterTime(Date date,String formatter) {
		//设置Date输出格式
		SimpleDateFormat sdf = new SimpleDateFormat(formatter);
		return sdf.format(date);
	}
	
	/**
	 * 获取当前时间后的时间
	 * @param hours
	 * @return
	 */
	public static String plusHoursToTime(long hours) {
		return LocalDateTime.now().plusHours(hours).format(dtf);
	}
	
	/**
	 * 获取当前时间前的时间
	 * @param hours
	 * @return
	 */
	public static String minusHoursToTime(long hours) {
		return LocalDateTime.now().minusHours(hours).format(dtf);
	}
	
	/**
	 * 获取给定时间后8小时的时间
	 * @param time
	 * @return
	 */
	public static String getRealTime(String time) {
		return getTimeAfter(time, 8);
	}
	
	/**
	 * 获取给定时间后的时间
	 * @param time
	 * @param hours
	 * @return
	 */
	public static String getTimeAfter(String time, long hours) {
		return LocalDateTime.from(dtf.parse(time)).plusHours(hours).format(dtf);
	}
	
	/**
	 * 获取给定时间前的时间
	 * @param time
	 * @param hours
	 * @return
	 */
	public static String getTimeBefore(String time, long hours) {
		return LocalDateTime.from(dtf.parse(time)).minusHours(hours).format(dtf);
	}
	
	/**
	 * 获取当前时间和给定时间的间隔小时数
	 * @param time
	 * @return
	 */
	public static long getDurationHours(String time) {
		return Duration.between(LocalDateTime.from(dtf.parse(time)), LocalDateTime.now()).toHours();
	}
	
	/**
	 * 获取给定两个时间的间隔小时数
	 * @param firstTime
	 * @param secondTime
	 * @return
	 */
	public static long getDurationHours(String firstTime, String secondTime) {
		return Duration.between(LocalDateTime.from(dtf.parse(firstTime)), LocalDateTime.from(dtf.parse(secondTime))).toHours();
	}
	
	/**
	 * 获取当前时间和给定时间的间隔分钟数
	 * @param time
	 * @return
	 */
	public static long getDurationMinutes(String time) {
		return Duration.between(LocalDateTime.from(dtf.parse(time)), LocalDateTime.now()).toMinutes();
	}
	
	/**
	 * 获取给定两个时间的间隔分钟数
	 * @param firstTime
	 * @param secondTime
	 * @return
	 */
	public static long getDurationMinutes(String firstTime, String secondTime) {
		return Duration.between(LocalDateTime.from(dtf.parse(firstTime)), LocalDateTime.from(dtf.parse(secondTime))).toMinutes();
	}
	
	/**
	 * 获取当前时间和给定时间的间隔天数
	 * @param time
	 * @return
	 */
	public static long getDurationDays(String time) {
		return Duration.between(LocalDateTime.from(dtf.parse(time)), LocalDateTime.now()).toDays();
	}
	
	/**
	 * 获取给定两个时间的间隔天数
	 * @param firstTime
	 * @param secondTime
	 * @return
	 */
	public static long getDurationDays(String firstTime, String secondTime) {
		return Duration.between(LocalDateTime.from(dtf.parse(firstTime)), LocalDateTime.from(dtf.parse(secondTime))).toDays();
	}


	/**
	 * 获取比较时间是否早于传入时间
	 * @param
	 * @param compareTime
	 * @return
	 */
	public static boolean isBefore(String compareTime,String currentTime){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DTF_FORMATTER);
		Boolean isBefore = Boolean.FALSE;
		try {
			Date compareDate = simpleDateFormat.parse(compareTime);
			Date currentDate = simpleDateFormat.parse(currentTime);
			isBefore = compareDate.before(currentDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return isBefore;
	}

    /**
     * 获取传入时间追加秒数后的时间字符串
     * @param time 传入时间
     * @param seconds 追加秒数
     * @return 时间 yyyy-MM-dd HH:mm:ss
     */
    public static String getTimeAfterSeconds(String time, long seconds) {
        return LocalDateTime.from(dtf.parse(time)).plusSeconds(seconds).format(dtf);
    }


    //秒转换成天   时   分  秒
	public static String formatDateTime(long mss) {
		String DateTimes = null;
		long days = mss / ( 60 * 60 * 24);
		long hours = (mss % ( 60 * 60 * 24)) / (60 * 60);
		long minutes = (mss % ( 60 * 60)) /60;
		long seconds = mss % 60;
		if(days>0){
			DateTimes= days + "天" + hours + "小时" + minutes + "分钟"
					+ seconds + "秒";
		}else if(hours>0){
			DateTimes=hours + "小时" + minutes + "分钟"
					+ seconds + "秒";
		}else if(minutes>0){
			DateTimes=minutes + "分钟"
					+ seconds + "秒";
		}else{
			DateTimes=seconds + "秒";
		}

		return DateTimes;
	}


	//两个时间相差值转换成秒数
	public static long getDurationSeconds(String oneTime,String twoTime){
    	long differenceSeconds = 0;
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date a = sdf.parse(oneTime);
			Date b = sdf.parse(twoTime);
			/*System.out.println((b.getTime() - a.getTime())/1000);
			System.out.println(LocalTimeUtil.formatDateTime((b.getTime() - a.getTime())/1000));*/
			differenceSeconds = (b.getTime() - a.getTime())/1000;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return differenceSeconds;
	}

	public static Date getDate(String timeStr,String pattern){
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			return sdf.parse(timeStr);
		} catch (Exception e) {
			log.error("日期转换出现异常：",e);
		}
		return null;
	}

	/**
	 * 获取真实的本地过期时间
	 * @param timeSeconds
	 * @return
	 */
	public static String getRealExpireTime(Long timeSeconds){
		LocalDateTime plusSeconds = LocalDateTime.now(ZoneId.of("Asia/Shanghai")).plusSeconds(timeSeconds);
		return plusSeconds.format(DateTimeFormatter.ofPattern(DTF_FORMATTER));
	}


	/**
	 * 获取真实时间的时间戳
	 *
	 * @return
	 */
	public static long getRealTimestamp() throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
		sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		return sdf.parse(sdf.format(new Date())).getTime();
	}

	public static String turnDayHourMinuteString(int minute) {
		//如果分钟小于60，默认返回分钟
		if (0 < minute && minute < 60) {
			return minute + "分钟";
		}
		//如果分钟小于24小时（1440分钟），返回小时和分钟
		if (60 <= minute && minute < 1440) {

			if (minute % 60 == 0) {
				int h = minute / 60;
				return h + "小时";
			} else {
				int h = minute / 60;
				int m = minute % 60;
				return h + "小时" + m + "分钟";
			}

		}
		//如果分钟大于1天
		if (minute >= 1440) {
			int d = minute / 60 / 24;
			int h = minute / 60 % 24;
			int m = minute % 60;
			String s1 = null;
			if (d > 0) {
				s1 = d + "天";
			}
			//h如果计算大于等于1再展示，否则只展示天和分钟
			if (h >= 1) {
				s1 += h + "小时";
			}
			if (m > 0) {
				s1 += m + "分钟";
			}

			return s1;
		}
		return null;
	}

}
